package com.itheima.interceptor;

import com.itheima.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author hu
 * @date 2025/3/12
 */
@Slf4j
//@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       /* //1.获取访问的资源路径uri
        String uri = request.getRequestURI();
        StringBuffer requestURL = request.getRequestURL();
        log.info("url-{}",requestURL);
        log.info("uri-{}",uri);
        //2.判断请求头中是否存在特殊路径
        if(uri.contains("/login")){
            return true;
        }*/
        //获取token
        String token = request.getHeader("token");
        //3.对token进行校验
        if (token==null || token.isEmpty()) {
            response.setStatus(401);
            return false;
        }
        try {
            JwtUtils.parseJWT(token);
        } catch (Exception e) {
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
